package com.hefu.virtuallife.ui;


import android.Manifest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;


import com.hefu.virtuallife.R;
import com.hefu.virtuallife.base.BaseActivity;

import com.hefu.virtuallife.base.MainLayoutPanel;
import com.hefu.virtuallife.customview.carousel.CarouselView;
import com.hefu.virtuallife.utils.StatusBarUtil;
import com.hefu.virtuallife.utils.UiUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;


import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.tv_info)
    TextView tv_info;
    @BindView(R.id.rg_main_parent)
    RadioGroup rg_main_parent;
    @BindView(R.id.gj_carouselView)
    CarouselView gjCarouselView;
    @BindView(R.id.sy_carouselView)
    CarouselView syCarouselView;
    @BindView(R.id.af_carouselView)
    CarouselView afCarouselView;
    @BindView(R.id.js_carouselView)
    CarouselView jsCarouselView;
    @BindView(R.id.yl_carouselView)
    CarouselView ylCarouselView;
    @BindView(R.id.jz_carouselView)
    CarouselView jzCarouselView;

    CarouselView lastView;

    //String[] carouselViewStrs;
    //int [] carouselViewDrawables;

    private int[] gj_drawables = {R.drawable.dc, R.drawable.ms, R.drawable.jp, R.drawable.jd, R.drawable.jj, R.drawable.jtcg};
    private int[] af_drawables = {R.drawable.wxhj, R.drawable.xqjk, R.drawable.mj, R.drawable.af, R.drawable.fk};
    private int[] sy_drawables = {R.drawable.sc, R.drawable.scp, R.drawable.zn, R.drawable.zs, R.drawable.zf};
    private int[] yl_drawables = {R.drawable.ys};
    private int[] js_drawables = {R.drawable.zcb, R.drawable.zh, R.drawable.fx, R.drawable.cs, R.drawable.fxy};
    private int[] jz_drawables = {R.drawable.cg, R.drawable.gcgl, R.drawable.sjs, R.drawable.aqy, R.drawable.jsy};

    @Override
    protected int setLayoutId() {
        return R.layout.activity_main;
    }



    @Override
    protected void initVariables(@Nullable Bundle savedInstanceState) {
        StatusBarUtil.setTransparent(this);

        //carouselViewStrs = UiUtils.getStringArray(R.array.gj_type);
        //carouselViewDrawables = gj_drawables;
        initCarouselViews();

        //initListen();
    }

    public void initCarouselViews(){
        String[] strs = UiUtils.getStringArray(R.array.gj_type);
        for (View stubItem : initData(strs, gj_drawables)) {
            gjCarouselView.addView(stubItem);
        }
        gjCarouselView.notifyDataSetChanged();

//        strs = UiUtils.getStringArray(R.array.sy_type);
//        for (View stubItem : initData(strs, sy_drawables)) {
//            syCarouselView.addView(stubItem);
//        }
//        syCarouselView.notifyDataSetChanged();
//        syCarouselView.setVisibility(View.GONE);
//
//        strs = UiUtils.getStringArray(R.array.af_type);
//        for (View stubItem : initData(strs, af_drawables)) {
//            afCarouselView.addView(stubItem);
//        }
//        afCarouselView.notifyDataSetChanged();
//        afCarouselView.setVisibility(View.GONE);
//
//        strs = UiUtils.getStringArray(R.array.js_type);
//        for (View stubItem : initData(strs, js_drawables)) {
//            jsCarouselView.addView(stubItem);
//        }
//        jsCarouselView.notifyDataSetChanged();
//        jsCarouselView.setVisibility(View.GONE);
//
//        strs = UiUtils.getStringArray(R.array.yl_type);
//        for (View stubItem : initData(strs, yl_drawables)) {
//            ylCarouselView.addView(stubItem);
//        }
//        ylCarouselView.notifyDataSetChanged();
//        ylCarouselView.setVisibility(View.GONE);
//
//        strs = UiUtils.getStringArray(R.array.jz_type);
//        for (View stubItem : initData(strs, jz_drawables)) {
//            jzCarouselView.addView(stubItem);
//        }
//        jzCarouselView.notifyDataSetChanged();
//        jzCarouselView.setVisibility(View.GONE);
//
//        lastView = gjCarouselView;
    }

    private List<View> initData(String[] carouselViewStrs, int[] drawables) {
        List<View> result = new ArrayList<>();

        int len = (carouselViewStrs.length < gj_drawables.length) ? carouselViewStrs.length : gj_drawables.length;
        String[] strs = new String[len];
        System.arraycopy(carouselViewStrs, 0, strs, 0, len);

        for (int i = 0; i < len; i++){
            MainLayoutPanel imagePanel = new MainLayoutPanel(this);
            imagePanel.setImageResId(drawables[i]);
            imagePanel.setText(strs[i]);
            result.add(imagePanel);
        }

        return result;
    }

    private void initListen() {
        rg_main_parent.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_house_keeper:
                        lastView.setVisibility(View.GONE);
                        lastView.scrollToChild(0);
                        gjCarouselView.setVisibility(View.VISIBLE);
                        lastView = gjCarouselView;
                        break;
                    case R.id.rb_business:
                        lastView.setVisibility(View.GONE);
                        lastView.scrollToChild(0);
                        syCarouselView.setVisibility(View.VISIBLE);
                        lastView = syCarouselView;
                        break;
                    case R.id.rb_security:
                        lastView.setVisibility(View.GONE);
                        afCarouselView.setVisibility(View.VISIBLE);
                        lastView = afCarouselView;
                        break;
                    case R.id.rb_military:
                        lastView.setVisibility(View.GONE);
                        lastView.scrollToChild(0);
                        jsCarouselView.setVisibility(View.VISIBLE);
                        lastView = jsCarouselView;
                        break;
                    case R.id.rb_medical_care:
                        lastView.setVisibility(View.GONE);
                        lastView.scrollToChild(0);
                        ylCarouselView.setVisibility(View.VISIBLE);
                        lastView = ylCarouselView;
                        break;
                    case R.id.rb_architecture:
                        lastView.setVisibility(View.GONE);
                        lastView.scrollToChild(0);
                        jzCarouselView.setVisibility(View.VISIBLE);
                        lastView = jzCarouselView;
                        break;
                    default:
                        break;
                }
            }
        });
    }

    public static final String TAG = "Permission" ;
    private void requestPermissions() {
        RxPermissions rxPermission = new RxPermissions(this);
        rxPermission
                .requestEach(Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_CALENDAR,
                        Manifest.permission.READ_CALL_LOG,
                        Manifest.permission.READ_CONTACTS,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.READ_SMS,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CAMERA,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.SEND_SMS)
                .subscribe(new io.reactivex.functions.Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // 用户已经同意该权限
                            Log.d(TAG, permission.name + " is granted.");
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                            Log.d(TAG, permission.name + " is denied. More info should be provided.");
                        } else {
                            // 用户拒绝了该权限，并且选中『不再询问』
                            Log.d(TAG, permission.name + " is denied.");
                        }
                    }
                });


    }

}
